package com.gwtapps.messenger.client.view;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtapps.messenger.client.view.ui.TitleCommandBar;
import com.gwtapps.messenger.client.model.Contact;
import com.gwtapps.messenger.client.model.Message;

public class ChatWindowView extends Composite{
	
	@SuppressWarnings("deprecation")
	private class InputListener extends KeyboardListenerAdapter{
		
		public void onKeyPress(Widget sender, char keyCode, int modifiers){
			if( keyCode == KeyboardListener.KEY_ENTER ){
				String text = input.getText();
				if( text.length()>0 ){
					Message message = new Message(text);
					addMessage( view.getContactList().getMe(), message );
					view.getListener().onSendMessage( contactTo, message );
					//view.getListener().onHit(contactTo);
					input.setText("");
				}
			}
		}
	}

	static final int CHAT_WINDOW_WIDTH = 250;
	static final int CHAT_WINDOW_HEIGHT = 200;
	static final int CHAT_WINDOW_MARGIN = 5;
	static final int CHAT_WINDOW_OFFSET = 20;

	private MessengerView view;
	private Contact contactTo;
	private ScrollPanel conversationScroller = new ScrollPanel();
	private VerticalPanel conversationPanel = new VerticalPanel();
	private TextBox input = new TextBox();
	int position = -1;
	
	public ChatWindowView( MessengerView view, Contact contactTo ){
		this.view = view;
		this.contactTo = contactTo;

		VerticalPanel mainPanel = new VerticalPanel();
		initWidget( mainPanel );

		TitleCommandBar titleBar;
		titleBar = new TitleCommandBar( contactTo.getName() );
		titleBar.addCommand("close", new ClickListener(){ 
			public void onClick( Widget sender ){ 
				ChatWindowView.this.view.closeChatWindow( ChatWindowView.this ); } });
		mainPanel.add(titleBar);		

		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setHeight("100%");
		vpanel.setWidth("100%");
		mainPanel.add( vpanel );
		mainPanel.setCellHeight(vpanel, "100%");

		conversationScroller.setHeight("150px");
		conversationScroller.setWidget( conversationPanel );
		
		vpanel.add( conversationScroller );
		vpanel.setCellHeight(conversationScroller, "100%");		

		conversationScroller.setWidget( conversationPanel );
		vpanel.add( input );
		input.setWidth("100%");
		input.addKeyboardListener( new InputListener() );

		setStyleName("chatWindow");
		vpanel.setStyleName("chatWindowContent");
		conversationScroller.setStyleName("chatWindowConversation");
		input.setStyleName("chatWindowInput");
		RootPanel.get().add( this );
	}


	public void addMessage( Message message ){
		addMessage( contactTo, message );
	}

	public void addMessage( Contact contact, Message message ){
		HTML messageLabel = new HTML("<b>" + contact.getName() + "</b>: " + message); 
		conversationPanel.add( messageLabel );
		conversationScroller.setScrollPosition(conversationPanel.getOffsetHeight());
		messageLabel.setStyleName("convMessage");
	}

	public void show(){
		setWidth( CHAT_WINDOW_WIDTH + "px" );
		setHeight(CHAT_WINDOW_HEIGHT + "px");
		setVisible( true );
		setPosition( position );
		input.setFocus( true );
	}
	
	public void setPosition( int position ){
		this.position = position;
		int top = Window.getClientHeight() +
		DOM.getIntAttribute(RootPanel.getBodyElement(), "scrollTop") -
		getOffsetHeight() - CHAT_WINDOW_MARGIN;		
		int left = Window.getClientWidth() +
		DOM.getIntAttribute(RootPanel.getBodyElement(), "scrollLeft") -
		CHAT_WINDOW_WIDTH*(position+1) - CHAT_WINDOW_MARGIN*(position+1) - CHAT_WINDOW_OFFSET;
		DOM.setStyleAttribute(getElement(),"position","absolute" );
		 DOM.setStyleAttribute(getElement(), "zIndex", "" + 9999);
		DOM.setStyleAttribute(getElement(),"top",Integer.toString( top ) );
		DOM.setStyleAttribute(getElement(),"left",Integer.toString( left ) );
		conversationScroller.setScrollPosition(conversationPanel.getOffsetHeight());
	}
}